package cn.ffcs.uec.staffAndOutStaff.action;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.Components;
import org.zkoss.zk.ui.event.Events;
import org.zkoss.zk.ui.event.ForwardEvent;

import cn.ffcs.raptornuke.plugin.common.zk.ctrl.BasePortletComposer;
import cn.ffcs.raptornuke.portal.theme.ThemeDisplay;
import cn.ffcs.uec.organization.constant.UecOrganizationConstant;
import cn.ffcs.uec.organization.manager.UecOrgDeptManager;
import cn.ffcs.uec.organization.manager.UecOrgOrgsManager;
import cn.ffcs.uec.organization.model.UecOrgDept;
import cn.ffcs.uec.organization.model.UecOrgOrgs;
import cn.ffcs.uec.organization.vo.OrganizationRelationVo;
import cn.ffcs.uec.staffAndOutStaff.action.bean.StaffTotalInfoListMainBean;
import cn.ffcs.uec.staffAndOutStaff.vo.StaffTotalInfo;
import cn.ffcs.uom.common.util.ApplicationContextUtil;
import cn.ffcs.uom.common.util.IPortletInfoProvider;
import cn.ffcs.uom.common.util.UomZkUtil;

@Controller
@Scope("prototype")
public class StaffTotalInfoListMainComposer extends BasePortletComposer implements IPortletInfoProvider {

    private static final long serialVersionUID = -616634567681376544L;

    private StaffTotalInfoListMainBean bean = new StaffTotalInfoListMainBean();

    private StaffTotalInfo staffTotalInfo = new StaffTotalInfo();

    /**
     * 组织树：组织+部门
     */
    private OrganizationRelationVo organizationRelationVo;

    /**
     * 选中的组织/部门 组织与部门分别在两张表中
     */
    private UecOrgOrgs uecOrgOrgs;

    private UecOrgDept uecOrgDept;

    private UecOrgOrgsManager uecOrgOrgsManager = (UecOrgOrgsManager) ApplicationContextUtil
            .getBean("uecOrgOrgsManager");

    private UecOrgDeptManager uecOrgDeptManager = (UecOrgDeptManager) ApplicationContextUtil
            .getBean("uecOrgDeptManager");

    @Override
    public String getPortletId() {
        return super.getPortletId();
    }

    @Override
    public ThemeDisplay getThemeDisplay() {
        return super.getThemeDisplay();
    }

    @Override
    public void doAfterCompose(Component comp) throws Exception {
        super.doAfterCompose(comp);
        UomZkUtil.autoFitHeight(comp);// 使用<borderlayout>时，需要加上这句进行初始化，不然从仓库调出的页面，不能显示
        Components.wireVariables(comp, bean);
        this.bean.getStaffTotalInfoListboxExt().setPortletInfoProvider(this);
        /**
         * 选中内部树上的（组织关系）
         */
        this.bean.getUecOrganizationTreeboxExt().addForward(UecOrganizationConstant.ON_SELECT_ORGREATION_TREE_REQUEST,
                this.self, "onSelectOrgOrDeptTreeResponse");
    }

    /**
     * 选中的内部树组织关系
     * 
     * @param event
     */
    public void onSelectOrgOrDeptTreeResponse(ForwardEvent event) {
        organizationRelationVo = (OrganizationRelationVo) event.getOrigin().getData();
        if (null != organizationRelationVo) {
            this.organizationVoTransfOrgOrDept(organizationRelationVo);
            organizationRelationVo = null;
        }
    }

    /**
     * 根据组织关系获取各对象（组织还是部门）
     */
    public void organizationVoTransfOrgOrDept(OrganizationRelationVo organizationRelationVo) {
        if ("ORG".equals(organizationRelationVo.getOrgType())) {
            uecOrgOrgs = uecOrgOrgsManager.findUecOrgOrgsByOrganizationRelationVo(organizationRelationVo);
            // 公司
            staffTotalInfo.setPkOrg(uecOrgOrgs.getPkOrg());
        } else {
            staffTotalInfo.setPkOrg(null);
        }
        if ("DEPT".equals(organizationRelationVo.getOrgType())) {
            uecOrgDept = uecOrgDeptManager.findUecOrgDeptByOrganizationRelationVo(organizationRelationVo);
            // 部门
            staffTotalInfo.setPkDept(uecOrgDept.getPkDept());
        } else {
            staffTotalInfo.setPkDept(null);
        }
        Events.postEvent("onOrgTreeOutStaffQuery", this.bean.getStaffTotalInfoListboxExt(), staffTotalInfo);
    }
}
